require("../css/register.less");

const axios = require('../utils/axiosUtil.js');

const utils = require('../utils/utils.js');

var CaptchaMini = require("captcha-mini");

window.onload = function () {
    let createCode = "";
    //验证码
    let captcha1 = new CaptchaMini();
    captcha1.draw(document.querySelector('#captcha'), function (res) {
        console.log(res, '验证码')
        createCode = res;
    });


    let btm = document.querySelector("button");
    let inps = document.querySelector(".inps");
    let inps1 = document.querySelector(".inps1");
    let inps2 = document.querySelector(".inps2");
    let inps3 = document.querySelector(".inps3");

    btm.addEventListener("click", function () {
        //非空验证
        if (!(inps.value && inps1.value && inps2.value && inps3.value)) {
            utils.toast(1,"请将数据输入完整");
            return;
        }
        // 正则验证 reg.test(tellInp.value) ==false 
        let reg = /^1[3-9][0-9]{9}$/;
        if (!reg.test(inps.value)) {
            utils.toast(1,'请输入正确的手机号');
            return;
        }
        // 一致性验证
        if (inps1.value != inps2.value) {
            utils.toast(1,'两次密码输入不一致！');
            return;
        }
        //验证码验证：
        console.log(createCode);
        if (inps3.value.toLowerCase() != createCode.toLowerCase()) {
            utils.toast(1,'请输入正确的验证码');
            return;
        }



        //组装数据
        let datas = {
            account: inps.value,
            password: inps1.value
        }
        axios.post('/api/user/register', datas).then(function (res) {
            if (res.data.errno == 0) {
                utils.toast(0,'注册成功,请登录');
                location.href = './login.html'
            } else {
                // 否则：将后端返回提示给用户
                utils.toast(1,res.data.message)
            }
        })
    })
    //点击登录
    let items = document.querySelector(".item");

    items.addEventListener("click", function () {
        location.href = './login.html'
    })
}